<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 * @package		Adminpro
 * @author		Tran Hoang Thien (thienhb12@gmail.com)
 * @copyright   PHP TEAM
 * @link		http://phpandmysql.net
 * @since		Version 1.0
 * @phone       0944418192
 *
 */
class Member extends MY_Controller {

	public function __construct(){
		parent:: __construct();
		$this->load->model('film_model');
		$this->load->model('muser');
		$this->load->library('form_validation');
		$this->load->model('category_model');
		$this->load->model('page_model');
		$this->load->model('config_model');
	}

	public function index(){	 
		$data                  = array();
		$data['config']        = $this->cache_site->get_info_cache($key = 'config',$time = 86400,$this->config_model->get_config_info());
		$data['title']         = 'My Profile';
		$data['keywords']      = $data['config']['cf_seo'];
		$data['description']   = $data['config']['cf_meta'];
		$data['phone']         = $data['config']['cf_phone'];
		$data['email']         = $data['config']['cf_email'];
		$data['result_cate']   = $this->cache_site->get_info_cache($key = 'cate',$time = 86400,$this->category_model->get_data_cate());
		$data['page']          = $this->cache_site->get_info_cache($key = 'page',$time = 86400,$this->page_model->get_list_page());
		$data['menu_template'] = 'member/myprofile';
		$data['template']      = 'member/member';
		$this->load->view("layout",$data);
	}

	public function change(){
		$data                  	 = array();
		$data['config']          = $this->cache_site->get_info_cache($key = 'config',$time = 86400,$this->config_model->get_config_info());
		$data['title']         	 = 'Change Password';
		$data['keywords']      	 = $data['config']['cf_seo'];
		$data['description']   	 = $data['config']['cf_meta'];
		$data['phone']         	 = $data['config']['cf_phone'];
		$data['email']         	 = $data['config']['cf_email'];
		$data['result_cate']     = $this->cache_site->get_info_cache($key = 'cate',$time = 86400,$this->category_model->get_data_cate());
		$data['page']            = $this->cache_site->get_info_cache($key = 'page',$time = 86400,$this->page_model->get_list_page());
		$request               	 = $this->input->post('change');
		$data['menu_template'] 	 = 'member/changepass';
		$data['template']      	 = 'member/member';
		if(isset($request)){
			$uid                 = $this->session->userdata('session_user_id');
			$info                = $this->muser->get_info($uid,array('password'));
			$old_password        = md5(fillter($this->input->post('old_password')));
			$new_password        = md5(fillter($this->input->post('new_password')));
			$repeat_new_password = md5(fillter($this->input->post('repeat_new_password')));
			$this->form_validation->set_rules('old_password','old password', 'required|trim|min_length[5]|max_length[12]|xss_clean');
			$this->form_validation->set_rules('new_password', 'new password','required|trim|xss_clean|min_length[5]|max_length[12]|');
			$this->form_validation->set_rules('repeat_new_password', 'repeat new password','required|trim|xss_clean|min_length[5]|max_length[12]|');
			if($this->form_validation->run() == FALSE){
				$data['menu_template']   = 'member/changepass';
				$data['template']        = 'member/member';
			}else{
				if($old_password != $info['password']){
					$data['error'] = '<div class="alert alert-danger rubberBand animated" role="alert">Old password incorrect!</div>';
				}else if($new_password  == $info['password']){
					$data['error'] = '<div class="alert alert-danger rubberBand animated" role="alert">The new password must not be identical old password!</div>';
				}else if($new_password != $repeat_new_password){
					$data['error'] = '<div class="alert alert-danger rubberBand animated" role="alert">Account is logged!</div>';	
				}else{
					$this->muser->save($uid,array('password'=>$new_password));
					$data['error'] =  '<div class="alert alert-success rubberBand animated" role="alert">Password is reset successfully!</div>';
				}
			}
		}
		$this->load->view("layout",$data);
	}

	public function list_film(){
		$user_id               = $this->session->userdata('session_user_id');
		$data_user             = $this->muser->get_info($user_id,array('filmlike','boxfilm'));	 
		$data                  = array();
		$data['config']        = $this->cache_site->get_info_cache($key = 'config',$time = 86400,$this->config_model->get_config_info());
		$data['title']         = 'List film';
		$data['keywords']      = $data['config']['cf_seo'];
		$data['description']   = $data['config']['cf_meta'];
		$data['phone']         = $data['config']['cf_phone'];
		$data['email']         = $data['config']['cf_email'];
		$data['result_cate']   = $this->cache_site->get_info_cache($key = 'cate',$time = 86400,$this->category_model->get_data_cate());
		$data['page']          = $this->cache_site->get_info_cache($key = 'page',$time = 86400,$this->page_model->get_list_page());
		if(!empty($data_user['filmlike'])){
			$filmlike                = explode(',',$data_user['filmlike']);
			$param_like              = array();
			$param_like['select']    = array('idphim','name','content','urlep_new','thumb','hot','decu','rewrite');
			$param_like['where']     = array('status' => 1);
			$param_like['where_in']  = $filmlike; 
			$data['result_filmlike'] = $this->film_model->get_list($param_like);
		}
		if(!empty($data_user['boxfilm'])){
			$filmbox                 = explode(',',$data_user['boxfilm']);
			$param_box               = array();
			$param_box['select']     = array('idphim','name','content','urlep_new','thumb','hot','decu','rewrite');
			$param_box['where']      = array('status' => 1);
			$param_box['where_in']   = $filmbox;   
			$data['result_boxfilm']  = $this->film_model->get_list($param_box);
		}
		$data['menu_template'] = 'member/list_film';
		$data['template']      = 'member/member';
		$this->load->view("layout",$data);
	}
}

/* End of file index.php */
/* Location: ./application/modules/home/controllers/index.php */